<?php
	/*
	 * @FileName: BuSignApi.class.php
	 * @Author: BuSign I.S.
	 * @Developers: Mario Lamers <info@busign.nl>
	 *              Sebas Lamers <sebasdevelopment@gmx.com>
	 *              
	 * @Desciption: This is the API endpoint for the BuSign API.
	 * 
	 * @package BuSign
	 * @class BuSignApi
	 */
	
	if ( floatval ( phpversion ( ) ) < 5.1 )
	{
		trigger_error ( 'You need at least PHP 5.1 to use the BuSign HCPanel API. Please contact your system administrator to upgrade PHP to the newest PHP version.', E_USER_ERROR );
	}
	
	if ( class_exists ( 'SoapClient' ) === false )
	{
		trigger_error ( 'The PHP SOAP extension is not installed. Please install PHP SOAP to use this API.', E_USER_WARNING );
	}
	
	if ( extension_loaded ( 'openssl' ) === false )
	{
		trigger_error ( 'The PHP OPENSSL extension is not installed. Please install PHP OPENSSL to use this API.', E_USER_WARNING );
	}
	
	if ( ini_get ( 'soap.wsdl_cache_enabled' ) !== false )
	{
		if ( ini_set ( 'soap.wsdl_cache_enabled', 0 ) === false )
		{
			trigger_error ( 'Cannot change ini settings, try via htaccess.', E_USER_WARNING );
		}
	}
	
	// Load all the BuSign API files.
	require_once 'BuSignApiSettings.class.php';
	require_once 'BuSignApiDomainService.class.php';
	//require_once 'BuSignApiWebhostingService.class.php';
	//require_once 'BuSignApiVPSService.class.php';
	//require_once 'BuSignApiGameService.class.php';

	class BuSignApi
	{
		// Some small API settings. (Don't change!)
		const WSDL = 'https://www.hcp.busign.nl/API/wsdl';
		protected static $_soapClient = null;
		
		const AVAILABILITY_ACCOUNT     = 'avail_account';
		const AVAILABILITY_UNAVAILABLE = 'unavailable';
		const AVAILABILITY_TAKEN       = 'taken';
		const AVAILABILITY_FREE        = 'free';
		const REGISTRATION_GONE        = 'gone';
		
		/*
		 * Gets the singleton SoapClient which is used to connect to the BuSign API
		 * 
		 * @return SoapClient the SoapClient object to which we can connect to the BuSign API
		 */
		public static function _getBuSignSoapClient ( )
		{
			// Connect to the BuSign IS Soap Server.
			if ( is_null ( self::$_soapClient ) )
			{
				$options = array (
				                  'trace' => BuSignApiSettings::$debug
				                 ,'encoding' => 'utf-8'
				                 ,'features' => SOAP_SINGLE_ELEMENT_ARRAYS // See http://bugs.php.net/bug.php?id=43338
				);
				
				self::$_soapClient = new SoapClient ( self::WSDL, $options );
				
				self::$_soapClient->__setCookie ( 'login', BuSignApiSettings::$loginHash );
				self::$_soapClient->__setCookie ( 'auth',  BuSignApiSettings::$authcode  );
				self::$_soapClient->__setCookie ( 'mode',  BuSignApiSettings::$mode      );
			}
			
			return self::$_soapClient;
		}
	}
	
	// Testing purpose.
	var_dump ( BuSignApi::_getBuSignSoapClient()->doTest( true ) );
	var_dump ( BuSignApi::_getBuSignSoapClient()->getDomainAuthCode ( 'busign.nl' ) );
	var_dump ( BuSignApi::_getBuSignSoapClient()->__getFunctions ( ) );
	
	try {
	$domain = new BuSign_Domain ( 'busign.nl' );
	var_dump ( $domain->GetDomainAuthCode ( ) );
	} catch ( Exception $e )
	{
		var_dump ( BuSignApi::_getBuSignSoapClient()->__getLastResponse ( ) );
	}
?>